import { dict } from '@fast-crud/fast-crud';
import { useMessage, useDialog } from 'naive-ui';
import { GET, DELETE, POST, PUT } from '@/utils/http/service';

// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
export default function ({ expose }) {
  const message = useMessage();
  const dialog = useDialog();
  return {
    crudOptions: {
      request: {
        pageRequest: async (query) => await GET(`/authority/applications`, query),
        addRequest: async ({ form }) => await POST(`/authority/applications`, form),
        editRequest: async ({ form }) =>
          await PUT(`/authority/applications/${form.clientId}`, form),
        delRequest: async ({ row }) => await DELETE(`/authority/applications/${row.clientId}`),
      },
      rowHandle: { fixed: 'right' },
      table: {
        rowKey: (row) => row.clientId,
      },
      columns: {
        _index: {
          title: '序号',
          form: { show: false },
          column: {
            align: 'center',
            width: '80px',
            columnSetDisabled: true,
            formatter: (context) => {
              const index = context.index ?? 1;
              const pagination = expose.crudBinding.value.pagination;
              return ((pagination.currentPage ?? 1) - 1) * pagination.pageSize + index + 1;
            },
          },
        },
        clientId: {
          title: '客户标识',
          type: 'text',
          search: { show: true },
          column: { ellipsis: true, width: 100 },
          form: {
            rule: [{ required: true, message: '客户标识不能为空', trigger: 'blur' }],
            component: {
              maxlength: 10,
              props: {
                type: 'text',
                showWordLimit: true,
              },
            },
          },
          editForm: {
            rule: [{ min: 2, max: 5, message: '客户标识长度为2-5', trigger: 'blur' }],
            component: { disabled: true },
          },
        },
        clientSecret: {
          title: '客户秘钥',
          type: 'text',
          column: { width: 100, show: false },
          form: {
            rule: [{ required: true, message: '客户秘钥不能为空', trigger: ['blur'] }],
          },
        },
        clientName: {
          title: '客户名称',
          type: 'text',
          column: { ellipsis: true, width: 150 },
          search: { show: true },
          form: {
            rule: [{ required: true, message: '客户名称不能为空', trigger: ['blur'] }],
          },
        },
        accessTokenValidity: {
          title: '登录时效',
          type: 'number',
          column: { ellipsis: true, width: 80 },
          addForm: {
            value: 43200,
          },
          form: {
            component: {
              addonAfter: '毫秒',
            },
            rule: [{ required: true, message: 'Token有效期不能为空', trigger: ['blur'] }],
            helper: 'Token有效期,默认12小时(1小时 = 3600毫秒 = 60 * 60)',
          },
        },
        status: {
          title: '应用状态',
          type: 'dict-switch',
          addForm: { value: true },
          column: {
            width: 80,
            align: 'center',
            component: {
              name: 'fs-dict-switch',
              vModel: 'value',
            },
            valueChange: ({ value, row }) => {
              dialog.warning({
                title: '提示',
                content: `确定${row.status ? '启用' : '禁用'}吗?`,
                positiveText: '确定',
                negativeText: '取消',
                onPositiveClick: () => {
                  PUT(`/authority/applications/${row.clientId}/${row.status}`).then(() => {
                    message.success(row.status ? '启用成功' : '禁用成功', { duration: 3 });
                  });
                },
                onNegativeClick: () => {
                  row.status = !value;
                },
              });
            },
          },
          dict: dict({
            data: [
              { value: true, label: '启用', color: 'success' },
              { value: false, label: '禁用', color: 'warning' },
            ],
          }),
          form: {
            rule: [{ required: true, message: '应用状态不能为空' }],
          },
        },
        scope: {
          title: '授权范围',
          type: 'dict-radio',
          addForm: { value: 'all' },
          column: { width: 100 },
          dict: dict({
            data: [
              { value: 'all', label: '全部', color: 'warning' },
              { value: 'server', label: '服务端', color: 'success' },
              { value: 'client', label: '客户端', color: 'success' },
            ],
          }),
          form: {
            rule: [{ required: true, message: '授权范围不能为空' }],
          },
        },
        type: {
          title: '应用类型',
          type: 'dict-select',
          addForm: { value: 0 },
          column: { width: 100 },
          dict: dict({
            data: [
              { value: 0, label: '综合应用', color: 'warning' },
              { value: 1, label: '服务应用', color: 'success' },
              { value: 2, label: 'PC网页', color: 'success' },
              { value: 3, label: '手机应用', color: 'success' },
              { value: 4, label: '小程序应用', color: 'success' },
            ],
          }),
          form: {
            rule: [{ required: true, message: '应用类型不能为空' }],
          },
        },
        authorizedGrantTypes: {
          title: '授权类型',
          type: ['dict-select'],
          form: {
            rule: [{ required: true, message: '授权范围不能为空' }],
            col: { span: 24 },
            labelCol: { span: 2 },
            wrapperCol: { span: 9 },
            component: { multiple: true },
          },
          valueBuilder({ value, row, key }) {
            if (value != null) {
              row[key] = value.split(',');
            }
          },
          valueResolve({ key, value, form }) {
            if (value) {
              form[key] = form[key].toString();
            }
          },
          dict: dict({
            data: [
              { value: 'password', label: '密码模式', color: 'success' },
              { value: 'authorization_code', label: '授权码模式', color: 'success' },
              { value: 'client_credentials', label: '客户端模式', color: 'warning' },
              { value: 'refresh_token', label: 'RefreshToken', color: 'warning' },
            ],
          }),
          column: { ellipsis: true, width: 350 },
        },
        additionalInformation: {
          title: '附加说明',
          type: ['textarea', 'colspan'],
          column: { ellipsis: true },
        },
        webServerRedirectUri: {
          title: '回调地址',
          type: ['textarea', 'colspan'],
          column: { ellipsis: true, show: false },
        },
        refreshTokenValidity: {
          title: 'RefreshToken有效期',
          type: 'text',
          column: { ellipsis: true, width: 180, show: false },
          form: {
            show: false,
            component: {
              addonAfter: '毫秒',
            },
          },
        },
        resourceIds: {
          title: '资源ID（微服务名）',
          type: 'text',
          column: { show: false },
          form: { show: false },
        },
        authorities: {
          title: 'authorities',
          type: 'text',
          column: { show: false },
          form: { show: false },
        },
        autoApprove: {
          title: '自动批准',
          type: 'text',
          column: { show: false },
          form: { show: false },
        },
      },
    },
  };
}
